/* =========================================================
 * ecp.component.loading.js
 * =========================================================
 * 等待动画
 * Copyright(c) 1998 - 2016 YGSoft.Inc, All Rights Reserved
 * Created by wugang5 on 2016/3/15.
 * ========================================================= */
define(["jquery","ecp.utils.i18n"],function($,i18n){
		"use strict";
		 //默认值
		 var defaults = {
			 timeout : 0,
			 container : $("body")
		 };
		 var template = {
			 all : '<div id ="overallLoadBox@serialid" class="overallLoadBox" style="width: {width}px; height: {height}px;filter: alpha(opacity=6);opacity: 0.6;">'+
					  '<div class="overallLoad">'+
						 '<div class="dot1"></div>'+
						 '<div class="dot2"></div>'+
					  '</div>'+
 				   '</div>',
			 part :'<div id ="partLoadBox@serialid" class="partLoadBox">'+
			 	  	 '<div class="partLoad"></div>'+
			 	   '</div>'
		};
		function getSerialNo(){
			return new Date().getTime();
		}
		function PageLoading(options){
			 if(this instanceof PageLoading){
				 var id = getSerialNo();
				 var options = $.extend({}, defaults, options || {});
				 var container = options.container;
				 //显示动画
				 this.show = function(){
					var div = "";
					var $element;
					if(!container || container.length === 0){return;}
					//默认加载到body下
					if (container[0].tagName === "BODY"){
						var $window = $(window);
						var $winHeight = $window.height(); 
						var $winWidth = $window.width();
						div = template.all.replace(/@serialid/ig, id).replace(/{width}/ig,$winWidth).replace(/{height}/ig, $winHeight);
						container.append(div);
						$element = $('#overallLoadBox' + id, container);
						this.$element = $element;
						$element.show();
					} else { 
						 div = template.part.replace(/@serialid/ig,id);
						 container.append(div);
						 $element = $('#partLoadBox' + id, container);
						 this.$element = $element;
						 $element.show();
					}
					//生成动画效果后默认不自动销毁
					var that = this;
					if(options.timeout && $.isNumeric(options.timeout) && options.timeout > 0){
						 setTimeout(function(){
							 that.hide();
						 }, options.timeout);
					}
				 };
				//隐藏动画
				 this.hide = function(){
					 this.$element.remove();
				 };
			 }else{
				 return new PageLoading(options);
			 }
	 };
	 (function($) {
		 $.fn.ecpLoading = function(option){
			 return this.each(function(){
				 var $this = $(this),
				 	data = $this.data('pageLoading'),
				 	options = $.extend({}, defaults, $this.data(), {container : $this}, typeof option === 'object' && option);
				 if (!data) {
					 $this.data('pageLoading', (data = new PageLoading(options)));
					 data.show();
				 }
				 if(typeof option === 'string'){
					 data[option].apply(data, []);
				 }
			 });
	 	};
	}(jQuery));

	return PageLoading;
})
